//
//  AViewController.m
//  DrawWall
//
//  Created by gll on 12-12-28.
//  Copyright (c) 2012年 gll. All rights reserved.
//

#import "AViewController.h"
#import "MyView.h"
#import "MediaImproveController.h"
@interface AViewController ()
@property (strong,nonatomic)  MyView *drawView;
@property (assign,nonatomic)  BOOL buttonHidden;
@property (assign,nonatomic)  BOOL widthHidden;
- (IBAction)retakePictureAction:(id)sender;

- (IBAction)deleteAction:(id)sender;
- (IBAction)editImageAction:(id)sender;

@end

@implementation AViewController
@synthesize detailImage = _detailImage;
//保存线条颜色
static NSMutableArray *colors;

//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES];
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    colors=[[NSMutableArray alloc]initWithObjects:[UIColor greenColor],[UIColor blueColor],[UIColor redColor],[UIColor blackColor],[UIColor whiteColor], nil];
    CGRect viewFrame=self.view.frame;
    self.buttonHidden=YES;
    self.widthHidden=YES;
    self.drawView=[[MyView alloc]initWithFrame:viewFrame];
    [self.drawView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview: self.drawView];
    [self.view sendSubviewToBack:self.drawView];
    
    CGFloat newY = 65.f;
    CGFloat newH = CGRectGetHeight(self.view.bounds) - newY * 2.f ;
    UIImageView *imgV = [[UIImageView alloc] initWithImage:_detailImage];
    [imgV setFrame:CGRectMake(0, newY, CGRectGetWidth(self.view.bounds), newH)];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];

    
	// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)remove:(id)sender{
    [ self.drawView revocation];
}
-(IBAction)back:(id)sender{
    [ self.drawView refrom];
}
-(IBAction)clear:(id)sender{
    [self.drawView clear];
}
-(IBAction)changeColors:(id)sender{
    if (self.buttonHidden==YES) {
        for (int i=1; i<6; i++) {
            UIButton *button=(UIButton *)[self.view viewWithTag:i];
            button.hidden=NO;
            self.buttonHidden=NO;
        }
    }else{
        for (int i=1; i<6; i++) {
            UIButton *button=(UIButton *)[self.view viewWithTag:i];
            button.hidden=YES;
            self.buttonHidden=YES;
        }
    
    }
    
    
}
-(IBAction)changeWidth:(id)sender{
    if (self.widthHidden==YES) {
        for (int i=11; i<15; i++) {
            UIButton *button=(UIButton *)[self.view viewWithTag:i];
            button.hidden=NO;
            self.widthHidden=NO;
        }
    }else{
        for (int i=11; i<15; i++) {
            UIButton *button=(UIButton *)[self.view viewWithTag:i];
            button.hidden=YES;
            self.widthHidden=YES;
        }
        
    }

}
- (IBAction)widthSet:(id)sender {
    UIButton *button=(UIButton *)sender;
    [self.drawView setlineWidth:button.tag-10];
}

- (IBAction)saveScreen:(id)sender {
    
    [[self.view viewWithTag:1000] setHidden:YES];
    [[self.view viewWithTag:1001] setHidden:YES];
    
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
    //截屏成功
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"" message:@"Save OK" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
    [alertView show];
    [[self.view viewWithTag:1000] setHidden:NO];
    [[self.view viewWithTag:1001] setHidden:NO];
//    [self.navigationController setNavigationBarHidden:NO];
    MediaImproveController *mediaImpVC = [[MediaImproveController alloc] initWithNibName:@"MediaImproveController" bundle:nil];
    [mediaImpVC setBtnDetailImg:image];
    [self.navigationController pushViewController:mediaImpVC animated:YES];
}
- (IBAction)colorSet:(id)sender {
    UIButton *button=(UIButton *)sender;
    [self.drawView setLineColor:button.tag-1];
    self.ColorButton.backgroundColor=[colors objectAtIndex:button.tag-1];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)dealloc {
//    [super dealloc];
//}
- (IBAction)retakePictureAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)deleteAction:(id)sender {
}

- (IBAction)editImageAction:(id)sender {
}
@end
